package com.liang.one;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/17
 * Time: 16:59
 * Description:
 */

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者 接受消息
 */
public class Consumer {

    //队列的名称
    public static final String QUEUE_NAME = "hello";


    //接受消息
    public static void main(String[] args) throws IOException, TimeoutException {

        //创建连接工厂

        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("119.29.13.67");
        factory.setUsername("admin");
        factory.setPassword("970830");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();



        //声明 接受消息
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                System.out.println(new String(message.getBody()));
            }
        };

        //声明 消费被中断的回调方法
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                System.out.println("消息 消费被中断!");
            }
        };

        //消费者 接受消息



        /**
         * 1 消费哪个队列
         * 2 消费成功后是否要自动应答
         * 3 消费者未成功消费 的回调
         * 4 消费者 取消消费的回调
         */
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);



    }

}